package com.config;

import cn.hutool.json.JSONUtil;
import org.apache.ibatis.cache.Cache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class RedisCache implements Cache {
    /**
     * 日志工具类
     */
    private static final Logger logger = LoggerFactory.getLogger(RedisCache.class);

    /**
     * 读写锁
     */
    private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
    /**
     * ID
     */
    private String id;

    /**
     * 集成redisTemplate
     */
    @Autowired
    private static RedisTemplate redisTemplate;

    public static void setRedisTemplate(RedisTemplate redisTemplate) {
        RedisCache.redisTemplate = redisTemplate;
    }
    public RedisCache() {
    }

    public RedisCache(String id) {
        if (id == null) {
            throw new IllegalArgumentException("Cache instances require an ID");
        } else {
            logger.debug("MybatisRedisCache.id={}", id);
            this.id = id;
        }
    }


    @Override
    public String getId() {
        return this.id;
    }

    @Override
    public int getSize() {
        try {
            Long size = redisTemplate.opsForHash().size(this.id.toString());
            logger.debug("MybatisRedisCache.getSize: {}->{}", id, size);
            return size.intValue();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public void putObject(final Object key, final Object value) {
        try {

            logger.debug("MybatisRedisCache.putObject: {}->{}->{}", id, key, JSONUtil.toJsonStr(value));
            redisTemplate.opsForHash().put(this.id.toString(), key.toString(), value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public Object getObject(final Object key) {
        try {
            Object hashVal = redisTemplate.opsForHash().get(this.id.toString(), key.toString());
            logger.debug("MybatisRedisCache.getObject: {}->{}->{}", id, key, JSONUtil.toJsonStr(hashVal));
            return hashVal;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Object removeObject(final Object key) {
        try {
            redisTemplate.opsForHash().delete(this.id.toString(), key.toString());
            logger.debug("MybatisRedisCache.removeObject: {}->{}->{}", id, key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void clear() {
        try {
            redisTemplate.delete(this.id.toString());
//            redisTemplate.opsForHash().delete()
            logger.debug("MybatisRedisCache.clear: {}", id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public ReadWriteLock getReadWriteLock() {
        return this.readWriteLock;
    }

    @Override
    public String toString() {
        return "MybatisRedisCache {" + this.id + "}";
    }



    /*private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
    @Autowired
    private RedisTemplate redisTemplate;

    private String id;

    private static JedisPool pool;

    private Integer timeout;

    public RedisCache(final String id) {
        if (id == null) {
            throw new IllegalArgumentException("Cache instances require an ID");
        }
        this.id = id;
    }

    @Override
    public String getId() {
        return this.id;
    }

    @Override
    public int getSize() {
        return 1;
    }

    @Override
    public void putObject(final Object key, final Object value) {
        redisTemplate.opsForValue().set(key, value);
    }

    @Override
    public Object getObject(final Object key) {
        return redisTemplate.opsForValue().get(key);

    }

    @Override
    public Object removeObject(final Object key) {
        return redisTemplate.delete(key);
    }

    @Override
    public void clear() {

    }

    @Override
    public ReadWriteLock getReadWriteLock() {
        return readWriteLock;
    }

    @Override
    public String toString() {
        return "Redis {" + id + "}";
    }

    public void setTimeout(Integer timeout) {
        this.timeout = timeout;
    }*/

}
